package cn.jingdalei.user.controller;

import cn.jingdalei.basic.exception.BusinessException;
import cn.jingdalei.basic.util.AjaxResult;
import cn.jingdalei.basic.util.PageList;
import cn.jingdalei.user.domain.User;
import cn.jingdalei.user.domain.UserDto;
import cn.jingdalei.user.query.UserQuery;
import cn.jingdalei.user.service.ILogininfoService;
import cn.jingdalei.user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

// @Controller
@RestController  // (controller + responseBoby[返回json格式] )
@RequestMapping("/user")
@Api(tags = "用户接口",description = "用户接口详细描述")
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private ILogininfoService logininfoService;

    /**
     * 添加或者修改
     * @param user
     * @return
     */
    @PutMapping
    @ApiOperation(value = "用户添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody  User user){
        try {
            if (user.getId()==null){
                userService.add(user);
            }else {
                userService.update(user);
            }
            return AjaxResult.me();
        } catch (Exception e){
            e.printStackTrace();
            // 避免多次创建对象
            return  AjaxResult.me().setMessage("操作失败。");
        }
    }
    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除",notes = "如果有id就是修改否则就是添加")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            userService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            // 链式编程思想
            return AjaxResult.me().setMessage("删除失败!");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            userService.patchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            // 链式编程思想
            return AjaxResult.me().setMessage("删除失败!");
        }
    }

    /**
     * 查询单个
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询",notes = "如果有id就是修改否则就是添加")
    public User findById(@PathVariable("id") Long id){
        return userService.queryById(id);
    }

    /**
     *  查询所有
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询全部用户信息",notes = "如果有id就是修改否则就是添加")
    public List<User> findAll(){
        return userService.queryAll();
    }

    /**
     * 分页查询
     * axios get不支持传对象
     * @param query
     * @return
     */
    @PostMapping         // @GetMapping()不能传对象
    @ApiOperation(value = "分页查询用户信息",notes = "如果有id就是修改否则就是添加")
    public PageList<User> findPage(@RequestBody UserQuery query){
        return userService.queryData(query);
    }

    @PostMapping("/register/phone")
    public AjaxResult register(@RequestBody UserDto userDto){
        try {
            userService.register(userDto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙，稍后重试");
        }
    }
}
